package com.ling.pl.security.view.role.component;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.view.View;
import com.bstek.dorado.view.ViewState;
import com.bstek.dorado.view.manager.ViewConfig;
import com.bstek.dorado.web.DoradoContext;
import com.ling.core.common.utils.ViewManagerHelper;
import com.ling.core.view.ViewComponent;
import com.ling.core.view.builder.IControlBuilder;
import com.ling.pl.security.feign.DefaultRoleService;
import com.ling.pl.security.feign.DefaultUrlService;
import com.ling.pl.security.model.DefaultUrlVO;
import com.ling.pl.security.model.UrlComponentVO;
import com.ling.pl.security.view.role.RoleMaintain;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author bo.wang
 * @since 2018-01-07
 */
@Component("ling2.roleComponentMaintain")
public class RoleComponentMaintain extends RoleMaintain implements
        InitializingBean, ApplicationContextAware {

    ApplicationContext applicationContext;
    private Collection<IControlBuilder> builders;

    @Resource
    DefaultRoleService roleService;
    @Resource
    DefaultUrlService urlService;

    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @DataProvider
    public Collection<DefaultUrlVO> loadUrls(String parentId, String roleId)
            throws Exception {
        return roleService.loadUrls(parentId, roleId);
    }

    @SuppressWarnings("unchecked")
    @DataProvider
    public List<ViewComponent> loadViewComponents(String viewName, String urlId, String roleId) throws Exception {
        if (StringUtils.isEmpty(viewName)) {
            return Collections.EMPTY_LIST;
        }
        List<ViewComponent> components = new ArrayList<ViewComponent>();
        if (viewName.toLowerCase().endsWith(".ling")) {
            viewName = viewName.substring(0, viewName.length() - 5);
        }

        String VIEWSTATE_KEY = ViewState.class.getName();
        DoradoContext context = DoradoContext.getCurrent();
        context.setAttribute(VIEWSTATE_KEY, ViewState.rendering);
        try {
            ViewConfig viewConfig = ViewManagerHelper.getViewConfig(context, viewName);
            if (viewConfig == null) {
                return components;
            }
            ViewComponent root = new ViewComponent();
            components.add(root);
            root.setName(View.class.getSimpleName());
            root.setIcon("url(skin>common/icons.gif) 0px -20px");
            root.setEnabled(false);
            View view = viewConfig.getView();
            if (view == null) return Collections.EMPTY_LIST;
            for (com.bstek.dorado.view.widget.Component component : view.getChildren()) {
                for (IControlBuilder builder : builders) {
                    if (builder.support(component)) {
                        builder.build(component, root);
                        break;
                    }
                }
            }
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("urlId", urlId);
            map.put("roleId", roleId);
            List<UrlComponentVO> urlComponents = urlService.loadUrlsByUrlIdAndroleId(urlId, roleId);
            buildViewComponents(root.getChildren(), urlComponents);
            return components;
        } finally {
            context.setAttribute(VIEWSTATE_KEY, ViewState.servcing);
        }
    }

    @Expose
    public void insertUrlComponents(Map<String, Object> parameter) throws Exception {
        urlService.insertUrlComponents(parameter);
    }

    private void buildViewComponents(Collection<ViewComponent> viewComponents, List<UrlComponentVO> urlComponents) {
        for (ViewComponent vc : viewComponents) {
            for (UrlComponentVO uc : urlComponents) {
                if (vc.getId().equals(uc.getComponent().getId())) {
                    vc.setUse(true);
                    vc.setAuthorityType(uc.getAuthorityType());
                    break;
                }
            }
            buildViewComponents(vc.getChildren(), urlComponents);
        }
    }

    public void afterPropertiesSet() throws Exception {
        builders = applicationContext.getBeansOfType(IControlBuilder.class).values();
    }
}
